package main

import "fmt"

func noChange(arr [3]int) {
	// 值传递, 原变量不会改变
	arr[0] = 10
	fmt.Println(arr)
}

func change(arr *[3]int) {
	arr[0] = 10
	fmt.Println(*arr)
}

func changeSlice(slice []int) {
	slice[0] = 100
	fmt.Printf("b: %p\n", slice)

}

func main() {
	// 初始化数组
	var arr0 [5]int = [5]int{1, 2, 3, 4, 5}
	var arr1 = [5]int{1, 2, 3, 4, 5}
	var arr2 = [...]int{1, 2, 3, 4, 5}           // 使用...将自动计算数组长度(仅可在初始化时使用)
	var arr3 = [5]string{0: "hello", 1: "world"} // 通过下标初始化
	var arr4 = [...]struct {
		name string
		age  int
	}{
		{
			name: "Dong",
			age:  18,
		},
		{
			name: "Xi",
			age:  1000,
		},
	}
	fmt.Printf("%v\n%v\n%v\n%v\n%+v\n", arr0, arr1, arr2, arr3, arr4)

	arr := [3]int{1, 2, 3}
	fmt.Println(arr)
	noChange(arr)
	fmt.Printf("no change value: %v\n", arr)

	change(&arr)
	fmt.Printf("change value: %v\n", arr)

	for i, v := range arr {
		fmt.Printf("%d:%d\n", i, v)
	}

	for i := 0; i < len(arr); i++ {
		fmt.Printf("%d:%d\n", i, arr[i])
	}

	slice := []int{1, 2, 3}
	fmt.Printf("a: %p\n", slice)
	changeSlice(slice)
	fmt.Println(slice)

	// 声明切片
	var s1 []int
	if s1 == nil {
		fmt.Println("1")
	} else {
		fmt.Println("2")
	}

	// 初始化
	s2 := []int{}
	fmt.Println(s2)

	s3 := make([]int, 0)
	fmt.Println(s3)

	var s4 []int = make([]int, 0, 10)
	fmt.Printf("s4 : %v\n", s4)

	s5 := arr[:]
	fmt.Println(s5)

	fmt.Printf("s5 cap : %d\n", cap(s5))
	fmt.Printf("s4 cap : %d\n", cap(s4))

	sm1 := make([]int, 9, 10)
	fmt.Printf("sm1 len:%d cap:%d\n", len(sm1), cap(sm1))
	sm1 = append(sm1, []int{1, 2, 3}...)
	fmt.Printf("sm1 len:%d cap:%d\n", len(sm1), cap(sm1))
	sm1 = append(sm1, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}...)
	fmt.Printf("sm1 len:%d cap:%d\n", len(sm1), cap(sm1))

}
